<?php /*
	
*/ ?>
<?php include("system/config.inc.php");
$donotinclude = 1;
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}

$msg= "";	
// INSERT into database.
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit") {
			$http="http://";
			$title=mysql_real_escape_string($_REQUEST['bannertitle']);
			$url=$http.mysql_real_escape_string($_REQUEST['url']);
			$varthpath = $_FILES['imagefile']['name'];
			$cat=$_REQUEST['parentId'];
			$expire=$_REQUEST['exipiredate'];				
	if(($_FILES['imagefile']['size'] > 2000000) || ($_FILES['imagefile']['size'] <= 0))	{					
		header("location:banner.php?msg=imgszbg");
		die();
	} else {				
				$insert="INSERT INTO `tblbanners` ( `varbannertitle` , `varbannerurl`,`varbannerimage`,`intbannercategory`,`dtexpiredate`,`dtadded`,`dtstatuschange`,`intstatus`) VALUES ('$title', '$url','$varthpath','$cat','$expire',NOW(),NOW(),'0')";
				$sql=$obj_db->insert($insert);
				// get Id of inserted record
				$screen_id = mysql_insert_id($obj_db->CONN);
				// Image uploading				
			if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && $_FILES['imagefile']['name']!="")	{
				$targetpath = "../images/banner/";
				$ext=$_FILES['imagefile']['name'];
				$ext=explode(".",$ext);
				if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")	{
				
					if($_FILES['imagefile']['size'] <= 2000000)	{
						$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
						$fl_db=$ext[0].$screen_id.".".$ext[1];
						if(file_exists($filename))	{
							chmod($filename, 0777);
							unlink($filename);
						}
						if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename)) {
							$sql= "UPDATE tblbanners SET varbannerimage = '".$fl_db."' WHERE intbannerid = '".$screen_id."' ";
							$res=$obj_db->sql_query($sql);
						}					
					
					}
				}
			}
			header("location:banner.php?msg=add");
			die();
	}
}


// UPDATE the record
$p_title="";
$p_url="";
$p_cat = "";
$p_expire="";
$action=1;
if((isset($_REQUEST['a']) && trim($_REQUEST['a'])==2) && (!(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['bannerid']) && trim($_REQUEST['bannerid'])!="")
 	{
		$screen_id =  mysql_real_escape_string($_REQUEST['bannerid']);
		$sql = "select * from tblbanners where intbannerid=$screen_id";
		$result = $obj_db->select($sql);
		if(count($result)<=0)
		{
			echo " No Record Found!<br>";
			die();
		}
		if($result)
		{
			foreach($result as $row)
			{
				$p_title=stripslashes($row['varbannertitle']);
				$p_url=stripslashes($row['varbannerurl']);
				$p_cat = stripslashes($row['intbannercategory']);
				$p_expire=stripslashes($row['dtexpiredate']);
				$action=2;
			}
		}
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update") {
				$title=mysql_real_escape_string($_REQUEST['bannertitle']);
				$url=mysql_real_escape_string($_REQUEST['url']);
				$varthpath = $_FILES['imagefile']['name'];
				$cat=mysql_real_escape_string($_REQUEST['parentId']);
				$expire=$_REQUEST['exipiredate'];
				
			if(($_FILES['imagefile']['size'] > 2000000)) {			
				header("location:banner.php?msg=imgszbg");
				die();
			} else {
					$sql_upd ="UPDATE tblbanners SET  
						varbannertitle = '$title',
						varbannerurl='$url',
						intbannercategory='$cat',
						dtexpiredate='$expire',
						dtadded=dtadded,
						dtstatuschange=dtstatuschange
						 where intbannerid ='$screen_id'";
			$result = $obj_db->edit($sql_upd);
			$action=1;
			// update images
				if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && $_FILES['imagefile']['name']!="")
				{
					$targetpath = "../images/banner/";
					$ext=$_FILES['imagefile']['name'];
					$ext=explode(".",$ext);
					if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG") {
						if($_FILES['imagefile']['size'] <= 2000000)	{
							$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
							$fl_db=$ext[0].$screen_id.".".$ext[1];
							
							if(file_exists($filename)) {
								chmod($filename, 0777);
								unlink($filename);
							}
							if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename)) {
								$sql= "UPDATE tblbanners SET varbannerimage = '".$fl_db."' WHERE intbannerid = '".$screen_id."' ";
								$res=$obj_db->sql_query($sql);
							}
						} else {
							header("location:banner.php?msg=imgszbg");
							die();
						}
					}
				}
			}		
		header("location:banner.php?msg=add");
		die();	 		
		}
	}
}


// DELETE record from database
if(isset($_REQUEST['a']) && trim($_REQUEST['a'])==3)
{
	if(isset($_REQUEST['bannerid']) && trim($_REQUEST['bannerid'] != ""))
	{
		$bannerid = mysql_real_escape_string($_REQUEST['bannerid']);
		$sql = "SELECT * FROM tblbanners WHERE intbannerid = '$bannerid'";
		
		$result = $obj_db->sql_query($sql);
		if(count($result)>0)
		{
			for($i=0;$i<count($result);$i++)
			{
				$targetpath = "../images/banner/";
				$th_file = $result[$i]['varbannerimage'];
				$filename1=$targetpath.$th_file;
					chmod($filename1, 0777);
					unlink($filename1);
			}
		}
		$sql_del = "Delete from `tblbanners` WHERE intbannerid ='$bannerid'";
		$del = mysql_query($sql_del);
		header("location:banner.php");
		die();
	}
}
?>
<?php include("inc/header.php");?>
<body>
<link href="css/cal.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="js/scw.js"></script>
<script language="javascript" src="js/banner.js"></script>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" class="middlebackground">
  <tr>
    <td colspan="3" valign="top" align="center"><?php include(INC."top.inc.php");?></td>
  </tr>
  <tr>
    <td width="2%">&nbsp;</td>
    <td width="95%"><?php   
   if(isset($_REQUEST['script']))
	{
	if(trim($_REQUEST['script'])=='addbanner' || trim($_REQUEST['script'])=='editbanner')
	{
	?>
		<form action="" method="post" enctype="multipart/form-data" name="banner">
		
          <table width="60%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
            <tr class="bg1">
              <td colspan="5" align="center"><strong>
                <?php if($_REQUEST['script']=='addbanner'){echo "Add New Banner...";}
	if($_REQUEST['script']=='editbanner'){echo "Edit Banner...";}?>
              </strong></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right">(* All fields are Required)</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td width="2%">&nbsp;</td>
              <td width="29%" align="right" class="sltimg"> *Banner Title</td>
              <td width="3%">:</td>
              <td width="62%" align="left"><label>
                <input name="bannertitle" type="text" id="bannertitle" value="<?php echo stripslashes($p_title);?>" />
              </label></td>
              <td width="4%">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="right" class="sltimg">*Link URL </td>
              <td>:</td>
              <td align="left"><input name="url" type="text" id="url" value="<?php if (strlen($p_url)<1) { echo "http://";}?><?php echo stripslashes($p_url);?>" /></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="right" class="sltimg">*Image</td>
              <td>:</td>
              <td align="left"><input name="imagefile" type="file" id="imagefile" /></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="right" class="sltimg">*Banner Category </td>
              <td align="left">:</td>
              <td align="left"><select name="parentId" id="parentId">
                  <option value="0">Root</option>
                  <?php 
					if($action == 2)
					{
						$selected = $parent_id;
					}else{
						$selected = 0;
					}	
					echo GetChild(0,0,$selected,$obj_db,1);
				?>
              </select></td>
              <td align="left">&nbsp;</td>
            </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="right" class="sltimg">*Expire Date </td>
              <td align="left">:</td>
              <td align="left"><input name="exipiredate" type="text" id="exipiredate" value="<?php echo stripslashes($p_expire);?>"  onMouseOver="scwShow(this,this);"/></td>
              <td align="left">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="5" align="left">&nbsp;
                  <input type="hidden" name="uploading" value="imageupload" /></td>
            </tr>
            <tr>
              <td colspan="5" align="center"><input name="Submit" type="submit" class="btn" id="Submit" value="<?php echo ($action==2) ? "Update":"Submit"; ?>"  onClick="return check();"/></td>
            </tr>
            <tr>
              <td colspan="5" align="center">&nbsp;</td>
            </tr>
          </table>
	  </form>
          <?php
	}
} else {
?>
<br />
<br />
<table width="70%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
      <tr class="bg1">
        <td colspan="5" align="left"><strong>Banners</strong><a href="banner.php?script=addbanner" class="aa"></a>...</td>
        <td colspan="3" align="right"><a href="banner.php?script=addbanner" class="aa">[NEW]</a></td>
        </tr>
      <tr>
	  <form method="post" name="pag">
	  <?php 
	   $rows="";
	 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!="")
	 {
	 	$rows=$_REQUEST['page'];
	 
	  }	
	  ?>
        <td colspan="6" align="left">Row per Page:
          <select name="page" onChange="document.pag.submit();">
		  <option value="0">All</option>
		  <option value="5"<?php if($rows=='5'){echo "selected";}else{echo "";}?>>5 Per Page</option>
		  <option value="10"<?php if($rows=='10'){echo "selected";}else{echo "";}?>>10 Per Page</option>
		  <option value="15"<?php if($rows=='15'){echo "selected";}else{echo "";}?>>15 Per Page</option>
        </select>
        </td>
		</form>
        </tr>
      <tr class="tblbg3">
        <td align="center"><strong>Name</strong></td>
        <td align="center"><strong>Image</strong></td>
        <td align="center"><strong>URL</strong></td>
        <td align="center"><strong>Status</strong></td>
        <td align="center"><strong>Edit</strong></td>
        <td align="center"><strong>Delete</strong></td>
      </tr>
      <?php
	  
	  // change status disable or active
if(isset($_REQUEST['status']))
{
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==0)
	 {
		if(isset($_REQUEST['id']) && trim($_REQUEST['id']!=""))
		{
			$id=$_REQUEST['id'];
			$update = $obj_db->sql_query("update tblbanners set intstatus = 1 where intbannerid=$id");
			header("location:banner.php");
			die();
		}
	}
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==1)
	{
		if(isset($_REQUEST['id']) && trim($_REQUEST['id']!=""))
		{
			$id=$_REQUEST['id'];
			$update = $obj_db->sql_query("update tblbanners set intstatus = 0 where intbannerid=$id");
			header("location:banner.php");
			die();
		}
	}
}
	 
	  /**************************************
				PAGING CODE START
		**************************************/
		 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!=0)
	 	{
			$rows=$_REQUEST['page'];
			$rowperpage=$rows;
		}	
			$tablename="tblbanners";
			$per_page_keywords= "";
			$per_page_sorts="";
			include("system/paging.inc.php");

		/**************************************
				PAGING CODE ENDING
		**************************************/
		
								
$sql="select * from tblbanners Limit ".($page_no*$row_per_page).",".$row_per_page;
$result=$obj_db->select($sql);
  		if($result)
		{
   		for($i=0;$i<count($result);$i++)
   		{
            $id=$result[$i]['intbannerid'];
			$imagepath = "../images/banner/th/";
			$limit=$result[$i]['varbannerimage'];
			$imagename = $imagepath.$limit;
			?>
      <tr class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';"  onmouseout="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">
        <td align="center"><?php echo $result[$i]['varbannertitle'];?></td>
        <td align="center"><img src="../images/banner/<?php echo stripslashes($result[$i]['varbannerimage']);?>"/></td>
        <td align="center"><?php echo stripslashes($result[$i]['varbannerurl']);?></td>
         <td align="center"><table border="0" cellspacing="0" cellpadding="0" align="center">
				  <tr>
		<?php 
					if($result[$i]['intstatus']==0)
					{
						
						echo "<td><a class='aa' title='Click here to Enable Banner' href='banner.php?status=0&s=0&id=$id'>Enable</a></td>";		
					}
					if($result[$i]['intstatus']==1)
					{
						
						echo "<td><a class='aa' title='Click here to Disable Banner' href='banner.php?status=1&s=1&id=$id'>Disable</a></td>";
					}
				?>
				</tr></table></td>
        <td align="center"><a Title="Click here to Edit" class="link" href="banner.php?script=editbanner&amp;a=2&amp;bannerid=<?php echo $result[$i]['intbannerid']; ?>"><img src="images/edit.bmp" border="0"/></a></td>
        <td align="center"><a Title="Click here to Delete" href="banner.php?a=3&amp;bannerid=<?php echo $result[$i]['intbannerid']; ?>" onClick="return confirm('Are you sure to delete this record?');"><img src="images/delete.bmp" border="0"/></a></td>
      </tr>
      <?php }}?>
      <tr>
        <td colspan="6" align="center"><?php pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page);?></td>
      </tr>
    </table>
    <?php }?>
	</td>
    <td width="3%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><?php include("inc/bottom.php");?></td>
  </tr>
</table>
</body></html>

